/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        // 迭代
        /*
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode ret = new ListNode(-1);
        ListNode dummy = ret;
        while (list1 != null && list2 != null) {
            if (list1.val > list2.val) {
                ret.next = list2;
                list2 = list2.next;
            }
            else {
                ret.next = list1;
                list1 = list1.next;
            }
            ret = ret.next;
            // System.out.println(dummy.next);
        }
        ret.next = list1 == null ? list2 : list1;
        return dummy.next;
        */

        // 递归
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }

        if (list1.val >= list2.val) {
            list2.next = Merge(list1, list2.next);
            return list2;
        }
        else {
            list1.next = Merge(list1.next, list2);
            return list1;
        }
    }
}
